package com.pe.rimac;

import java.util.ArrayList;
import java.util.List;

import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;

import com.pe.rimac.util.RSTextWatcher;

public class DetalleActivity extends BaseActivity {
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vs_detalle);
		
        final Spinner vehiculoSpinner = (Spinner) findViewById(R.id.cmbVehiculo);
	    vehiculoSpinner.setOnItemSelectedListener(this);
        
        cargarListaDetalles();       
        
    }	
	
	private void cargarListeners() {		
	    
	    final Spinner marcaSpinner = (Spinner) findViewById(R.id.cmbMarca);
		marcaSpinner.setOnItemSelectedListener(this);
		
		final Spinner tipoSpinner = (Spinner) findViewById(R.id.cmbTipo);
	    tipoSpinner.setOnItemSelectedListener(this);
		
		final Spinner modeloSpinner = (Spinner) findViewById(R.id.cmbModelo);
		modeloSpinner.setOnItemSelectedListener(this);
		
		final Spinner anhoSpinner = (Spinner) findViewById(R.id.cmbAnho);
		anhoSpinner.setOnItemSelectedListener(this);
		
		final EditText colorEditText = (EditText) findViewById(R.id.txtColor);
		colorEditText.addTextChangedListener(new RSTextWatcher(colorEditText, siniestro));
		
		final EditText motorEditText = (EditText) findViewById(R.id.txtMotor);
		motorEditText.addTextChangedListener(new RSTextWatcher(motorEditText, siniestro));
		
		final Spinner usoSpinner = (Spinner) findViewById(R.id.cmbUsoEncontrado);
		usoSpinner.setOnItemSelectedListener(this);
		
		final CheckBox timonCambiandoCheckBox = (CheckBox) findViewById(R.id.chkTimonCambiado);
		timonCambiandoCheckBox.setOnCheckedChangeListener(this);
		
		final Spinner ciaSpinner = (Spinner) findViewById(R.id.cmbCIA);
		ciaSpinner.setOnItemSelectedListener(this);
		
	}



	private void cargarListaDetalles() 
    {
		cargarTipoVehiculo();
    	cargarMarcas();
    	cargarAnhos();
    	//cargarCIAS();//corregir la tabla 
    	cargarUsoEncontrado();
    	cargarPlacas();
    }
	
	private void cargarUsoEncontrado() {
		cargarSpinner(R.id.cmbUsoEncontrado, "TBL_USOENCONTRADOVEHICULO", "CODUEVEH", "DUEVEH");
	}

	private void cargarPlacas() {
		cargarSpinner(R.id.cmbVehiculo, "Placa", "select distinct placa as _id, Placa from TBL_VEHICULOS WHERE CODSINIESTRO=?", new String[]{codigoSiniestro});
	}	
	
	private void cargarTipoVehiculo()
	{
		cargarSpinner(R.id.cmbTipo, "TBL_TIPOVEHICULO", "Tipo_Vehiculo", "DestipVeh");
	}
	
	private void cargarMarcas()
	{
		cargarSpinner(R.id.cmbMarca, "TBL_MARCAS", "CODMARCA", "DMARCA");		
	}
	
	private void cargarAnhos()
	{
		List<CharSequence> catalogo = new ArrayList<CharSequence>();
		
		ArrayAdapter<CharSequence> featuresAdapter = new ArrayAdapter<CharSequence>(this,
				  android.R.layout.simple_spinner_item, catalogo);
		
		featuresAdapter.setDropDownViewResource(
				  android.R.layout.simple_spinner_dropdown_item);
		
		for (int i = 1980; i <= 2011; i++) {
			featuresAdapter.add(String.valueOf(i));
		}
		
		Spinner s = (Spinner) findViewById(R.id.cmbAnho);
		s.setAdapter(featuresAdapter);
		
	}
	
	@SuppressWarnings("unchecked")
	private void selectedAnho(int idSpinner, String value)
	{
		final Spinner spinner = (Spinner) findViewById(idSpinner);
		ArrayAdapter<CharSequence> adapter = (ArrayAdapter<CharSequence>) spinner.getAdapter();
		
		for (int i = 0; i < adapter.getCount(); i++) {
			CharSequence item = adapter.getItem(i);
			if(value.equals(item.toString()))
			{
				spinner.setSelection(i);
				break;
			}
		}
	}
	
	private void cargarCIAS()
	{
		//FIXME correguir la tabla
		cargarSpinner(R.id.cmbCIA, "", "", "");
	}
	
	private void cargarModelos(String idMarca)
	{
		cargarSpinner(R.id.cmbModelo, "TBL_MODELOS", "COD_MODELO", "DMODELO", "WHERE COD_MARCA = ?", new String[] {idMarca});
		/*encontroModelo = selectedSpinner(R.id.spnModelo, siniestro.getVehiculo().getModelo().getValor());
		if(!encontroModelo)
		{
			alertaDepenencia(siniestro.getVehiculo().getMarca(), siniestro.getVehiculo().getModelo());
		}*/
	}
	
	private void llenarFormulario() {
		selectedSpinner(R.id.cmbTipo, siniestro.getVehiculo().getTipo().getValor());
		selectedSpinner(R.id.cmbMarca, siniestro.getVehiculo().getMarca().getValor());
		selectedSpinner(R.id.cmbUsoEncontrado, siniestro.getVehiculo().getUso().getValor());
		selectedAnho(R.id.cmbAnho, String.valueOf(siniestro.getVehiculo().getAnho()));
		setTextEditText(R.id.txtColor, siniestro.getVehiculo().getColor());
		setTextEditText(R.id.txtMotor, siniestro.getVehiculo().getMotor());
		flagChekBox(R.id.chkTimonCambiado, siniestro.getVehiculo().isTimonCambiado());
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		
		SQLiteCursor item = null;
		if(parent.getItemAtPosition(pos) instanceof SQLiteCursor)
		{
			item= (SQLiteCursor)parent.getItemAtPosition(pos);
		}
		if(parent.getId() == R.id.cmbVehiculo)
		{
			cargarVehiculo(item.getString(0));
			llenarFormulario();
			cargarListeners();
		}
		else if(parent.getId() == R.id.cmbTipo)
		{
			setBeanValorDescripcion(siniestro.getVehiculo().getTipo(), item);
		}
		else if(parent.getId() == R.id.cmbMarca)
		{
			cargarModelos(item.getString(0));
			setBeanValorDescripcion(siniestro.getVehiculo().getMarca(), item);
		}
		else if(parent.getId() == R.id.cmbModelo)
		{
			setBeanValorDescripcion(siniestro.getVehiculo().getModelo(), item);
		}
		else if(parent.getId() == R.id.cmbAnho)
		{
			int anho = Integer.parseInt(parent.getItemAtPosition(pos).toString());
			siniestro.getVehiculo().setAnho(anho);
		}
		else if(parent.getId() == R.id.cmbUsoEncontrado)
		{
			setBeanValorDescripcion(siniestro.getVehiculo().getUso(), item);
		}
		else if(parent.getId() == R.id.cmbCIA)
		{
			setBeanValorDescripcion(siniestro.getVehiculo().getCia(), item);
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		
	}
	
	@Override
	public void onClick(View v) {
	}
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(buttonView.getId() == R.id.chkTimonCambiado)
		{
			siniestro.getVehiculo().setTimonCambiado(getFlagCheckBox((CheckBox)buttonView));
		}
	}
	
}
